<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'顾问列表'),
	array('op'=>'edit','title'=>'添加顾问'),
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if($_RQ['title']){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['mobile']){
		$params['mobile like']='%'.$_RQ['mobile'].'%';
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total=''; 
	$list=pdo_getslice('jy_aixiaoyi_guwen',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'mid','title'=>'顾问ID','type'=>'string'),
			array('name'=>'member','title'=>'顾问信息','type'=>'member'),
			array('name'=>'title','title'=>'顾问名称','type'=>'string'),
			array('name'=>'logo','title'=>'顾问头像','type'=>'image'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'wechat','title'=>'微信号','type'=>'string'),
			array('name'=>'shetimes','title'=>'剩余次数','type'=>'string'),
			array('name'=>'isdefault','title'=>'默认官方','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),
		'action'=>array(
			array('type'=>'modal','op'=>'recharge','title'=>'充值次数'),
		)),
		
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'顾问名称','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'电话','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='recharge'){
	//充值
	$credit_types=credit_types();
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['addshetimes']){
			exi('请先输入充值次数！','error');
		}
		pdo_update('jy_aixiaoyi_guwen',array('shetimes +='=>$postdata['addshetimes']),array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('操作成功！','','list');
	}
	$id=$_RQ['id']?intval($_RQ['id']):intval($_RQ['mid']);
	if(!$id){
		exi('请先选择要充值的顾问！','error');
	}
	$return['item']=pdo_get('jy_aixiaoyi_guwen',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'顾问昵称','read'=>true),
			array('name'=>'shetimes','title'=>'剩余次数','type'=>'string','read'=>true),
			array('name'=>'addshetimes','title'=>'充值次数','type'=>'string'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aixiaoyi_guwen',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回用户列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'顾问名称','type'=>'string'),
			array('name'=>'logo','title'=>'顾问头像','type'=>'image'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'wechat','title'=>'微信号','type'=>'string'),
			array('name'=>'mid','title'=>'绑定会员','type'=>'choose','action'=>'core/member.get_member/'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
			
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>$postdata['title'],
		'logo'=>$postdata['logo'],
		'mobile'=>$postdata['mobile'],
		'wechat'=>$postdata['wechat'],
		'mid'=>intval($postdata['mid']),
		'status'=>intval($postdata['status'])
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_aixiaoyi_guwen',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_aixiaoyi_guwen',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_aixiaoyi_guwen',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的顾问！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_aixiaoyi_guwen',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isdefault'])){
		pdo_update('jy_aixiaoyi_guwen',array('isdefault'=>0),array('isdefault'=>1,'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_aixiaoyi_guwen',array('isdefault'=>intval($post['isdefault'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}